//
//  YXFNetDeviceTool.m
//  YXFMarket
//
//  Created by Leewon on 2020/9/16.
//  Copyright © 2020 信雅科技. All rights reserved.
//

#import "YXFNetDeviceTool.h"

@implementation YXFNetDeviceTool

+ (instancetype)netDeviceTool {
   static YXFNetDeviceTool *deviceTool;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        deviceTool = [[YXFNetDeviceTool alloc]init];
    });
    return deviceTool;
}

- (void)getRequireWithURL : (NSString *)url
    withCompletionHandler : (nonnull void (^)(NSDictionary *dataDic))completionHandler {
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];//[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"GET"];
    //    [request setAllHTTPHeaderFields:@{@"Content-Type":@"application/x-www-form-urlencoded"}];
    [request setAllHTTPHeaderFields:@{@"Accept":@"application/vnd.app.v1+json, application/json, text/plain, */*"}];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dic;
     
        if(data){
            
            dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            
            if(completionHandler){
                completionHandler(dic);
            }
        });
        NSLog(@"device get : %@",dic);
    }];
    [dataTask resume];
}

- (void)postRequireWithURL : (NSString *)url
                 withParam : (NSString *)paramString
     withCompletionHandler : (void(^)(NSDictionary *dataDic))completionHandler {
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];//[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];
    NSData *data = [paramString dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [request setAllHTTPHeaderFields:@{@"Accept":@"application/vnd.app.v1+json, application/json, text/plain, */*"}];
    
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSDictionary *dic;
     
        if(data){
            
            dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        }
        if(completionHandler){
            completionHandler(dic);
        }
        NSLog(@"device post : %@",dic);
    }];
    [dataTask resume];
}

#pragma mark - 开放获取设备秘钥接口
/**
 开放获取设备秘钥接口
 
 @param completionHandler 数据回调
 */
- (void)getDeviceKeyWithMerchantID : (NSString *)merchantID
             withCompletionHandler : (void(^)(NSDictionary *dataDic))completionHandler {
    
    NSString *url = [NSString stringWithFormat:@"http://mdevicewebtest.xinyartech.com/api/app/v1/merchant/%@/open-get-device",merchantID];//orderReturn/getReturnProductList
    [self getRequireWithURL:url withCompletionHandler:completionHandler];
}

#pragma mark - 激活设备(秘钥+MAC地址)
/**
 激活设备(秘钥+MAC地址)
 
 @param completionHandler 数据回调
 */
- (void)activeDeviceWithCompletionHandler : (void(^)(NSDictionary *dataDic))completionHandler {
    
    NSString *url = @"http://mdevicewebtest.xinyartech.com/api/app/v1/device/activate-device";
//    NSUUID *uuid = [UIDevice currentDevice].identifierForVendor;
//    NSString *uuidString = uuid.UUIDString;
    NSString *paramString = [NSString stringWithFormat:@"device_key=%@",kDeviceKey];
    [self postRequireWithURL:url withParam:paramString withCompletionHandler:completionHandler];
    //282A0360-761E-4D94-B756-71B2F3358FCA

}
#pragma mark - 设备日志上传
/**
 设备日志上传
 
 @param completionHandler 数据回调
 */
- (void)uploadLogWithInfo : (NSString *)logInfo
    withCompletionHandler : (void(^)(NSDictionary *dataDic))completionHandler {
    
    NSString *url = @"http://mdevicewebtest.xinyartech.com/api/app/v1/device/upload-log";
    NSString *paramString = [NSString stringWithFormat:@"device_key=%@&type=%@&log_string=%@",kDeviceKey,@1,logInfo];
//    [self postRequireWithURL:url withParam:paramString withCompletionHandler:completionHandler];
    
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10];//[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
    [request setHTTPMethod:@"POST"];
    NSData *data = [paramString dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [request setAllHTTPHeaderFields:@{@"Accept":@"application/vnd.app.v1+json, application/json, text/plain, */*"}];
    
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"back_identify"];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config];
    
    [[session uploadTaskWithStreamedRequest:request] resume];
}
@end
